package com.wechat.page.chat.popup;

import com.swak.fx.support.FXMLView;
import com.swak.fx.support.Window;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIAction;
import com.swak.fx.support.ui.UIButton;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UIRichText;
import com.wechat.page.MainPage;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

/**
 * AnnouncementPopup.
 * 
 * @author DELL
 */
@FXMLView(title = "公告", value = "AnnouncementPage.fxml", css = "AnnouncementPage.css", stageStyle = "TRANSPARENT")
public class AnnouncementPage extends Window {

	private HBox titlePane;
	private Label title;
	private Pane placeholder;

	// 控制按钮
	private UIAction topBtn;
	private UIAction minBtn;
	private UIAction closeBtn;

	// 富文本
	private UIRichText richText;

	// 按钮组
	private GridPane btns;
	private UIButton confirm;
	private UIButton cancel;

	public AnnouncementPage(Stage stage) {
		super(stage, MainPage.ADJUST_SIZE);
		this.setResizeAble(false);
		this.topBtn = new UIAction();
		this.minBtn = new UIAction();
		this.closeBtn = new UIAction();
		this.title = new Label("群公告");
		this.placeholder = new Pane();
		this.titlePane = new HBox();

		this.richText = new UIRichText();
		this.btns = new GridPane();
		this.confirm = new UIButton("确定").Success();
		this.cancel = new UIButton("取消");
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.initTitlePane();
		this.initRichTextPane();
		this.initBtnsPane();

		this.addChild(this.titlePane);
		GridPane.setConstraints(this.titlePane, 0, 0);
		GridPane.setHgrow(this.titlePane, Priority.ALWAYS);
		GridPane.setVgrow(this.titlePane, Priority.NEVER);

		this.addChild(this.richText);
		GridPane.setConstraints(this.richText, 0, 1);
		GridPane.setHgrow(this.richText, Priority.ALWAYS);
		GridPane.setVgrow(this.richText, Priority.ALWAYS);

		this.addChild(this.btns);
		GridPane.setConstraints(this.btns, 0, 2);
		GridPane.setHgrow(this.btns, Priority.ALWAYS);
		GridPane.setVgrow(this.btns, Priority.NEVER);

		GridPane.setMargin(this.titlePane,
				new Insets(MainPage.ADJUST_SIZE, MainPage.ADJUST_SIZE, 0, MainPage.ADJUST_SIZE));
		GridPane.setMargin(this.richText, new Insets(0, MainPage.ADJUST_SIZE, 0, MainPage.ADJUST_SIZE));
		GridPane.setMargin(this.btns, new Insets(0, MainPage.ADJUST_SIZE, MainPage.ADJUST_SIZE, MainPage.ADJUST_SIZE));
	}

	private void initTitlePane() {
		this.topBtn.textNodeProperty().set(new UIIcon(UI.Icons.THUMB_TACK));
		this.minBtn.textNodeProperty().set(new UIIcon(UI.Icons.MINUS));
		this.closeBtn.textNodeProperty().set(new UIIcon(UI.Icons.REMOVE));
		this.closeBtn.getStyleClass().add("close-btn");

		this.title.getStyleClass().add("title");
		this.titlePane.getStyleClass().add("title_pane");
		this.titlePane.getChildren().addAll(this.title, this.placeholder, this.topBtn, this.minBtn, this.closeBtn);
		HBox.setHgrow(this.placeholder, Priority.ALWAYS);
	}

	private void initRichTextPane() {
		this.richText.getStyleClass().add("rich_text");
	}

	private void initBtnsPane() {
		this.btns.getStyleClass().add("btns");
		this.btns.getChildren().addAll(this.confirm, this.cancel);
		GridPane.setConstraints(this.confirm, 0, 0);
		GridPane.setConstraints(this.cancel, 1, 0);

		GridPane.setHgrow(this.confirm, Priority.ALWAYS);
		GridPane.setHalignment(this.confirm, HPos.RIGHT);
		GridPane.setHgrow(this.cancel, Priority.ALWAYS);
		GridPane.setHalignment(this.cancel, HPos.LEFT);

		GridPane.setMargin(this.confirm, new Insets(0, 5, 0, 0));
		GridPane.setMargin(this.cancel, new Insets(0, 0, 0, 5));
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.registerUIEvent(this.titlePane, UI.Events.Window_Drag);
		this.registerUIEvent(this.topBtn, UI.Events.Window_Fix);
		this.registerUIEvent(this.minBtn, UI.Events.Window_Min);
		this.registerUIEvent(this.closeBtn, UI.Events.Window_Close);
		this.registerUIEvent(this.titlePane, UI.Events.Window_Max2);
		this.registerUIEvent(this.placeholder, UI.Events.Window_Drag);
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.fixActiveProperty().addListener((ob, o, n) -> {
			if (n) {
				this.topBtn.getStyleClass().add("active");
			} else {
				this.topBtn.getStyleClass().remove("active");
			}
		});
	}

	@Override
	public void windowStateChange(int state) {
		super.windowStateChange(state);
		this.fixActiveProperty().set((state & UI.States.Window_Fix.index) == UI.States.Window_Fix.index);
	}

	@Override
	public void close() {
		super.close();
		this.destory();
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private BooleanProperty fixActiveProperty = new SimpleBooleanProperty(false);

	public BooleanProperty fixActiveProperty() {
		return fixActiveProperty;
	}

	/**
	 * 创建窗口
	 * 
	 * @return
	 */
	public static AnnouncementPage New() {
		return AnnouncementPage.New(AnnouncementPage.class, Window.createStage(true));
	}
}
